//
//  RoseDetailView.m
//  ROSE
//
//  Created by 艾阳 on 2018/6/4.
//  Copyright © 2018年 HeNanZhongDunYunAn. All rights reserved.
//

#import "RoseDetailView.h"
#import "RoseModel.h"
#import "UIWindow+CurrentViewController.h"
#import "SVGModel.h"

@interface RoseDetailView()

@property (nonatomic,assign) RoseDetailType DetailType;


/**
 玫瑰图片
 */
@property (nonatomic,strong) UIImageView *roseImg;

/**
 能量
 */
@property (nonatomic,strong) UIButton * energy;

/**
 浇水
 */
@property (nonatomic,strong) UIButton * waterBtn;
/**
 属性
 */
@property (nonatomic,strong) UILabel *status;


/**
 age
 */
@property (nonatomic,strong) UILabel *days;

/**
 玫瑰类型
 */
@property (nonatomic,strong) UILabel *roseLX;

/**
 价格
 */
@property (nonatomic,strong) UILabel *price;

/**
 所有人
 */
@property (nonatomic,strong) UILabel *owner;


/**
 区块时间
 */
@property (nonatomic,strong) UILabel *blockTime;


/**
 区块ID
 */
@property (nonatomic,strong) UILabel *blockID;

/**
 属性名称
 */
@property (nonatomic,strong) NSArray * statusTitleArray;


/**
 属性label列表
 */
@property (nonatomic,strong) NSMutableArray *statusLabelArray;

@end

@implementation RoseDetailView
{
    UILabel * _endLabel;
}

- (NSArray *)statusTitleArray{
    if (!_statusTitleArray) {
        _statusTitleArray = @[@"花朵颜色:",@"花朵形状:",@"叶子颜色:",@"叶子形状:",@"根茎颜色:",@"根茎形状:"];
    }
    return _statusTitleArray;
}

- (NSMutableArray *)statusLabelArray{
    if (!_statusLabelArray) {
        _statusLabelArray = [[NSMutableArray alloc] init];
    }
    return _statusLabelArray;
}

- (instancetype)initWithType:(RoseDetailType)type{
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.DetailType = type;
        if (!_roseImg) {
            _roseImg = [[UIImageView alloc]init];
            _roseImg.contentMode = UIViewContentModeScaleAspectFit;
            _roseImg.backgroundColor = ZS_UIColorMakeWithRGBA(255, 228, 225, 1);
            [self addSubview:_roseImg];
        }
        if (!_waterBtn) {
            _waterBtn = [[UIButton alloc]init];
            [_waterBtn setTitle:@"浇水" forState:UIControlStateNormal];
            [_waterBtn setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
            [_waterBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [_waterBtn setImage:[UIImage imageNamed:@"watering"] forState:UIControlStateNormal];
            [self addSubview:_waterBtn];
        }
        
        if (!_energy) {
            _energy = [[UIButton alloc]init];
            [_energy setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
            [_energy.titleLabel setFont:[UIFont systemFontOfSize:18]];
            [_energy setImage:[UIImage imageNamed:@"vitality"] forState:UIControlStateNormal];
            [_energy setTitle:@"123456" forState:UIControlStateNormal];
            [self addSubview:_energy];
        }
        
        if(!_status){
            _status = [[UILabel alloc]init];
            _status.layer.cornerRadius = 5.0f;
            _status.layer.masksToBounds = YES;
            _status.backgroundColor = [UIColor colorWithRed:221/255.0 green:163/255.0
                                                       blue:4/255.0 alpha:1];
            _status.textColor = [UIColor whiteColor];
            _status.textAlignment = NSTextAlignmentCenter;
            _status.font = [UIFont systemFontOfSize:10];
            [self addSubview:_status];
        }
        if(!_days){
            _days = [[UILabel alloc]init];
            _days.layer.masksToBounds = YES;
            _days.layer.cornerRadius = 5.0f;
            _days.backgroundColor = [UIColor colorWithRed:233/255.0 green:123/255.0
                                                     blue:155/255.0 alpha:1];
            _days.textColor = [UIColor whiteColor];
            _days.textAlignment = NSTextAlignmentCenter;
            _days.font = [UIFont systemFontOfSize:10];
            [self addSubview:_days];
        }
        if (!_roseLX) {
            _roseLX = [[UILabel alloc]init];
            _roseLX.textColor = [UIColor blackColor];
            _roseLX.font = [UIFont boldSystemFontOfSize:18];
            _roseLX.backgroundColor = [UIColor clearColor];
            [self addSubview:_roseLX];
        }
        
        if (!_price) {
            _price = [[UILabel alloc]init];
            _price.textColor =  [UIColor themeColor];
            _price.font = [UIFont systemFontOfSize:15];
            [self addSubview:_price];
        }
        
        if (!_owner) {
            _owner = [[UILabel alloc]init];
            [_owner setFont:[UIFont systemFontOfSize:13]];
            [self addSubview:_owner];
        }
        
        
        [self makeLayout];
    }
    return self;
}


- (void)makeLayout{
    
    [self.roseImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(size_width);
        make.height.mas_equalTo((size_width)*1016.0/1240.0);
    }];
    /** 我的玫瑰--浇水*/
    if (self.DetailType==MyRoseDetail) {
        [self.waterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.roseImg.mas_bottom).with.offset(10);
            make.right.mas_equalTo(-30);
            make.height.mas_equalTo(25);
        }];
        [self.energy mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.roseImg.mas_bottom).with.offset(10);
            make.right.mas_equalTo(self.waterBtn.mas_left).with.offset(-30);
            make.height.mas_equalTo(25);
        }];
    }else{
        /** 市场玫瑰没有浇水*/
        [self.energy mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.roseImg.mas_bottom).with.offset(10);
            make.right.mas_equalTo(-30);
            make.height.mas_equalTo(25);
        }];
    }
    
    [self.status mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(self.energy.mas_bottom).with.offset(30);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(16);
    }];
    
    [self.days mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.status.mas_right).with.offset(5);
        make.top.mas_equalTo(self.energy.mas_bottom).with.offset(30);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(16);
    }];
    
    [self.roseLX mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(self.status.mas_bottom).with.offset(10.0f);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(20);
    }];
    
    [self.price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(self.roseLX.mas_bottom).with.offset(8.0f);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(16);
    }];
    
    /** 分隔线*/
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview: line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.price.mas_bottom).with.offset(20);
    }];
    
    [self.owner mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(line.mas_bottom).with.offset(20);
    }];
    /** 所有者*/
    UILabel *ownTitleLabel = [[UILabel alloc]init];
    ownTitleLabel.text = @"所有者";
    ownTitleLabel.textColor = [UIColor lightGrayColor];
    [ownTitleLabel setFont:[UIFont systemFontOfSize:13]];
    [self addSubview:ownTitleLabel];
    [ownTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(self.owner.mas_bottom).with.offset(5);
    }];
    
    /** 属性*/
    UILabel * statusHintLabel = [[UILabel alloc]init];
    statusHintLabel.text = @"属性";
    statusHintLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:statusHintLabel];
    [statusHintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(ownTitleLabel.mas_bottom).with.offset(20);
    }];
    
    /** 创建属性列表*/
    for (int i=0; i<6; i++) {
        UILabel * statusLabel = [[UILabel alloc]init];
        [statusLabel setFont:[UIFont systemFontOfSize:13]];
        [statusLabel setText:self.statusTitleArray[i]];
        [statusLabel setTextColor:[UIColor grayColor]];
        [self addSubview:statusLabel];
        [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            i%2==0?make.left.mas_equalTo(30):make.left.mas_equalTo(size_width/2+30);
            make.top.mas_equalTo(statusHintLabel.mas_bottom).with.offset((i/2+1)*15+(i/2)*18);
        }];
        if (i==5) {
            _endLabel = statusLabel;
        }
        /** 添加到label列表 方便赋值*/
        [self.statusLabelArray addObject:statusLabel];
    }
    
    /** 市场玫瑰详情*/
    if (self.DetailType==MarketRoseDetail) {
        
        if (!_buyButton) {
            /** 卖出*/
            _buyButton = [[UIButton alloc]init];
            [_buyButton setTitle:@"购买" forState: UIControlStateNormal];
            [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_buyButton setBackgroundColor:[UIColor themeColor] forState:UIControlStateNormal];
            _buyButton.layer.masksToBounds = YES;
            _buyButton.layer.cornerRadius = 20.0f;
            [_buyButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [self addSubview:_buyButton];
            [_buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_endLabel.mas_bottom).offset(20);
                make.left.mas_equalTo(35);
                make.right.mas_equalTo(-35);
                make.height.mas_equalTo(40);
                make.bottom.equalTo(_buyButton.superview.mas_bottom).offset(-30);
            }];
        }
    }else{
       /** 我的玫瑰详情*/
    }
    self.showsVerticalScrollIndicator = NO;
    
}

- (void)fillDataWithRoseModel:(RoseModel *)roseModel{
    [[UIWindow zf_currentViewController] setTitle: roseModel.MC];
    [self.roseImg setImage:[self stringToImage:roseModel.BASE64]];
    [self.energy setTitle:[NSString stringWithFormat:@"%@能量",roseModel.TLZ] forState:UIControlStateNormal];
    [self.status setText:roseModel.DJMC];
    self.roseLX.text = [NSString stringWithFormat:@"%@%@",roseModel.DJMC,roseModel.SXMC];
    self.owner.text = roseModel.SYRMC;
    self.days.text = [NSString stringWithFormat:@"%@天",roseModel.AGE];
    self.price.text = [NSString stringWithFormat:@"%@ROST",roseModel.SJJG];
    /**属性列表 */
    [self getRoseStatus:roseModel.ID];
    /**区块记录 */
    if (self.DetailType == MyRoseDetail) {
        [self getBlockList:roseModel.ID];
    }
}


/**
 64base字符串转图片
 */
- (UIImage *)stringToImage:(NSString *)str {
    
    NSData * imageData =[[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
    UIImage *photo = [UIImage imageWithData:imageData ];
    
    return photo;
}

#pragma mark - 获取玫瑰属性表
- (void)getRoseStatus:(NSString *)roseID{
    [[LMJRequestManager sharedManager] POST:[NSString stringWithFormat:@"%@/rose/getsvg",url_base] parameters:@{@"ROSEID":roseID} completion:^(LMJBaseResponse *response) {
        if (response.error==nil&&[response.responseObject[@"errorcode"] isEqualToString:@"000000"]) {
            SVGModel *svgModel = [[SVGModel alloc] init];
            [svgModel setValuesForKeysWithDictionary:response.responseObject[@"dataobj"]];
            [self fillStatusList:svgModel];
        }
    }];
}


/**
 为属性列表赋值
 */
- (void)fillStatusList:(SVGModel*)svgModel{
    NSArray * statusArray = @[svgModel.BW1,svgModel.BW2,svgModel.BW3,svgModel.BW4,svgModel.BW5,svgModel.BW6];
    for (int i=0; i<6; i++) {
        UILabel * Label = self.statusLabelArray[i];
        [Label setText:[Label.text stringByAppendingString:statusArray[i]]];
    }
}

#pragma mark -区块记录
- (void)getBlockList:(NSString *)roseID{
    UILabel *latesTitleLabel = [[UILabel alloc]init];
    [self addSubview:latesTitleLabel];
    latesTitleLabel.text = @"最近记录";
    latesTitleLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:latesTitleLabel];
    [latesTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(_endLabel.mas_bottom).with.offset(20);
    }];
    
    if (!_blockTime) {
        _blockTime = [[UILabel alloc]init];
        [_blockTime setFont:[UIFont systemFontOfSize:13]];
        [_blockTime setTextColor:[UIColor grayColor]];
        [self addSubview:_blockTime];
        [_blockTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.top.mas_equalTo(latesTitleLabel.mas_bottom).with.offset(15);
        }];
    }
    if (!_blockID) {
        _blockID = [[UILabel alloc]init];
        [_blockID setFont:[UIFont systemFontOfSize:13]];
        [_blockID setTextColor:[UIColor grayColor]];
        [self addSubview:_blockID];
        [_blockID mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(size_width/2+30);
            make.top.mas_equalTo(latesTitleLabel.mas_bottom).with.offset(15);
        }];
    }
    /** 区块记录*/
    [[LMJRequestManager sharedManager] POST:[NSString stringWithFormat:@"http://123.206.56.11:8080/BChain_Rose/RoseByID"] parameters:@{@"ID":roseID} completion:^(LMJBaseResponse *response) {
        if (response.error==NULL) {
            [self.blockID setText:[NSString stringWithFormat:@"区块：%@",response.responseObject[@"QKID"]]];
            [self.blockTime setText:[NSString stringWithFormat:@"时间：%@",[self timeStampToString:response.responseObject[@"CJSJ"]]]];
        }
    }];
    
    /** 调整按钮位置*/
//    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"赠送",@"卖出"]];
//    [segmentControl setTintColor:[UIColor themeColor]];
//    segmentControl.layer.cornerRadius = 10.0f;
//    segmentControl.layer.masksToBounds = YES;
//    [segmentControl setSelectedSegmentIndex:1];
//    segmentControl.layer.borderWidth = 0.5;
//    segmentControl.layer.borderColor = [UIColor themeColor].CGColor;
//    [self addSubview:segmentControl];
//    [segmentControl mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(30);
//        make.right.mas_equalTo(-30);
//        make.top.equalTo(_blockTime.mas_bottom).offset(20);
//        make.bottom.equalTo(segmentControl.superview.mas_bottom).offset(-30);
//    }];
    
    
    if (!_giveBtn) {
        /** 赠送*/
        _giveBtn = [[UIButton alloc]init];
        [_giveBtn setTitle:@"赠送" forState: UIControlStateNormal];
        [_giveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_giveBtn setBackgroundColor:[UIColor themeColor] forState:UIControlStateNormal];
        _giveBtn.layer.masksToBounds = YES;
        _giveBtn.layer.cornerRadius = 15.0f;
        [_giveBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self addSubview:_giveBtn];
        [_giveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_blockTime.mas_bottom).offset(20);
            make.left.mas_equalTo(35);
            make.right.mas_equalTo(-(size_width/2+10));
            make.height.mas_equalTo(35);
            make.bottom.equalTo(_giveBtn.superview.mas_bottom).offset(-30);
        }];


    }
    if (!_sellBtn) {
        /** 卖出*/
        _sellBtn = [[UIButton alloc]init];
        [_sellBtn setTitle:@"卖出" forState: UIControlStateNormal];
        [_sellBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sellBtn setBackgroundColor:[UIColor themeColor] forState:UIControlStateNormal];
        _sellBtn.layer.masksToBounds = YES;
        [_sellBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        _sellBtn.layer.cornerRadius = 15.0f;
        [self addSubview:_sellBtn];
        [_sellBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_blockTime.mas_bottom).offset(20);
            make.left.mas_equalTo(_giveBtn.mas_right).with.offset(20);
            make.right.mas_equalTo(-35);
            make.height.mas_equalTo(35);
            make.bottom.equalTo(_sellBtn.superview.mas_bottom).offset(-30);
        }];

    }
    
}


/**
 把时间戳转换成时间
 */
- (NSString *)timeStampToString:(NSString *)timeStampString{
    NSTimeInterval interval    =[timeStampString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    return dateString;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
